package com.cn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName uploadController
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/11/20 20:19
 * @Version 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class uploadController {

    @PostMapping("/upload")
    public  String upload(MultipartFile file){

        if(file.isEmpty()){
            return "图片为空";
        }
        String originalFileName=file.getOriginalFilename();
        ApplicationHome applicationHome=new ApplicationHome(this.getClass());
//        前置路径
        String pre=applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath()+
                "\\src\\main\\resources\\image\\";
        String path=pre+originalFileName;

        try{
            file.transferTo(new File(path) );
        }catch (IOException e){
            e.printStackTrace();
        }
        return  path;
    }
}
